#include "rulebase.h"
#include "easylogging/easylogging++.h"
#include "formulaanalyse.h"
#include "qdebug.h"
#include "QStringRef"

RuleBase::RuleBase()
{
    m_nID = -1;
}

RuleBase::~RuleBase()
{
    for(int i = 0; i < m_vConditions.size(); i++)
    {
        delete m_vConditions[i];
    }

    for(int i = 0; i < m_vformula.size(); i++)
    {
        delete m_vformula[i];
    }
}

void RuleBase::SetName(QString name)
{
    m_sName = name;
}

void RuleBase::AddConditon(Condition* condition)
{
    m_vConditions.push_back(condition);
}

RuleResult RuleBase::Run(QString entityName)
{
    RuleResult result(m_nID,m_sName);

    for(int i = 0; i < m_vConditions.count(); i++)
    {

        if(i == m_vConditions.count() - 1)
        {
           result.conditionResults.push_back(m_vConditions[i]->Run(m_nID, entityName,true));
        }
        else
        {
            result.conditionResults.push_back(m_vConditions[i]->Run(m_nID, entityName,false));
        }
    }

    for(int i = 0; i < m_vformula.size(); i++)
    {
        RuleFormula* ruleformula = m_vformula[i];
        result.result = FormulaAnalyse::Result(m_vConditions,ruleformula->GetFormula());
        if(result.result)
        {
            LDEBUG<<"规则："<< ruleformula->GetName().toUtf8().constData() << " 运行结果： 成功";

        }
        else
        {
            LDEBUG<<"规则："<< ruleformula->GetName().toUtf8().constData()<<" 运行结果： 失败";
        }
    }

    return result;
}

void RuleBase::AddFormula(RuleFormula* ruleFormula)
{
    m_vformula.push_back(ruleFormula);
}
